package com.example.tiantian.battery;

import com.example.tiantian.BaseActivity;
import com.example.tiantian.OnReceiveListener;
import com.example.tiantian.R;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class NewBatteryActivity extends BaseActivity implements OnClickListener, OnReceiveListener {

    TextView btnCharge;
    TextView btnInfo;

    FragmentManager fm;

    InfoFragment fInfo;
    ChargeFragment fCharge;

    BatteryReceiver receiver;
    IntentFilter filter;

    boolean isInfo;
    OnInformationListener mInfomationListner;

    Bundle extras;

    @Override
    protected void init() {
        setContentView(R.layout.activity_fragment);

        btnCharge = (TextView) findViewById(R.id.fragment1);
        btnInfo = (TextView) findViewById(R.id.fragment2);

        fInfo = new InfoFragment();
        fCharge = new ChargeFragment();

        fm = getFragmentManager();

        receiver = new BatteryReceiver();
        filter = new IntentFilter();

        isInfo = false;
    }

    @Override
    protected void loadData() {
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);

        btnCharge.setText(R.string.battery_charge);
        Drawable charge = getResources().getDrawable(R.drawable.battery_tab_tain_selected);
        charge.setBounds(0, 0, charge.getMinimumWidth(), charge.getMinimumWidth());
        btnCharge.setCompoundDrawables(null, charge, null, null);
        btnCharge.setCompoundDrawablePadding(5);

        btnInfo.setText(R.string.battery_info);
        Drawable info = getResources().getDrawable(R.drawable.battery_tab_info_selected);
        info.setBounds(0, 0, info.getMinimumWidth(), info.getMinimumWidth());
        btnInfo.setCompoundDrawables(null, info, null, null);
        btnInfo.setCompoundDrawablePadding(5);
    }

    @Override
    protected void event() {
        btnCharge.setOnClickListener(this);
        btnInfo.setOnClickListener(this);

        receiver.SetOnReceiveListener(this);

        registerReceiver(receiver, filter);

        changeFragment();
    }

    @Override
    public void onClick(View v) {
        isInfo = v.getId() == R.id.fragment2;
        changeFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void changeFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content, isInfo ? fInfo : fCharge);
        ft.commit();
        btnCharge.setEnabled(isInfo);
        btnInfo.setEnabled(!isInfo);

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        extras = intent.getExtras();
        if (mInfomationListner != null && extras != null) {
            mInfomationListner.onGetInformation(extras);
        }
    }

    public void setOnInformationListener(OnInformationListener onInformationListener) {
        mInfomationListner = onInformationListener;
    }

    interface OnInformationListener {
        public void onGetInformation(Bundle extras);
    }
}
